package cn.ggb.eduservice.controller;


import cn.ggb.commonutils.R;
import cn.ggb.eduservice.client.vodVideo;
import cn.ggb.eduservice.entity.EduVideo;
import cn.ggb.eduservice.service.EduVideoService;
import cn.ggb.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private vodVideo vodVideo;

    @Autowired
    private EduVideoService eduVideoService;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节，删除对应的视频
    @GetMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id查询视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id删除视频
            R result = vodVideo.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001){
                throw new GuliException(200001,"熔断");
            }
        }
        //删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }


    //修改小节
    @PostMapping("")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
}

